home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Mine Sweeper / Source / dialog.c next >
Text File  |  1994-04-26  |  9KB  |  428 lines

  1. /*    dialog.c
  2.  *
  3.  *        This is the preferences manager dialog box
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "mines.h"
  8.  
  9. /*    GetMyDialog
  10.  *
  11.  *        This gets my dialog box
  12.  */
  13.  
  14. DialogPtr GetMyDialog(short id)
  15. {
  16.     DialogPtr dlog;
  17.     short i1;
  18.     Handle i2;
  19.     Rect i3;
  20.     
  21.     dlog = GetNewDialog(id,NULL,(WindowPtr)-1);
  22.     if (dlog == NULL) return;
  23.     SetPort(dlog);
  24.     GetDItem(dlog,1,&i1,&i2,&i3);
  25.     InsetRect(&i3,-4,-4);
  26.     PenSize(3,3);
  27.     FrameRoundRect(&i3,16,16);
  28.     PenSize(1,1);
  29.     return dlog;
  30. }
  31.  
  32. /*    GetButton
  33.  *
  34.  *        Return button state
  35.  */
  36.  
  37. short GetButton(DialogPtr dlog, short id)
  38. {
  39.     short i1;
  40.     Handle i2;
  41.     Rect i3;
  42.     
  43.     GetDItem(dlog,id,&i1,&i2,&i3);
  44.     return GetCtlValue((ControlHandle)i2);
  45. }
  46.  
  47. /*    SetButton
  48.  *
  49.  *        Set button state
  50.  */
  51.  
  52. void SetButton(DialogPtr dlog, short id, short val)
  53. {
  54.     short i1;
  55.     Handle i2;
  56.     Rect i3;
  57.     
  58.     GetDItem(dlog,id,&i1,&i2,&i3);
  59.     SetCtlValue((ControlHandle)i2,val);
  60. }
  61.  
  62. /*    GetName
  63.  *
  64.  *        What is the user's name
  65.  */
  66.  
  67. void GetName(DialogPtr dlog, short id, char *c)
  68. {
  69.     short i1;
  70.     Handle i2;
  71.     Rect i3;
  72.     unsigned char buffer[256];
  73.     
  74.     GetDItem(dlog,id,&i1,&i2,&i3);
  75.     GetIText(i2,buffer);
  76.     if (buffer[0] > 31) buffer[0] = 31;
  77.     for (i1 = buffer[0]; i1 >= 0; i1--) c[i1] = buffer[i1];
  78. }
  79.  
  80. /*    SetValue
  81.  *
  82.  *        Set the value
  83.  */
  84.  
  85. void SetValue(DialogPtr dlog, short id, short val)
  86. {
  87.     unsigned char buffer[256];
  88.     short i1;
  89.     Handle i2;
  90.     Rect i3;
  91.     
  92.     NumToString(val,buffer);
  93.     GetDItem(dlog,id,&i1,&i2,&i3);
  94.     SetIText(i2,buffer);
  95. }
  96.  
  97. /*    GetValue
  98.  *
  99.  *        Get the value
  100.  */
  101.  
  102. short GetValue(DialogPtr dlog, short id)
  103. {
  104.     unsigned char buffer[256];
  105.     short i1;
  106.     Handle i2;
  107.     Rect i3;
  108.     long n;
  109.     
  110.     GetDItem(dlog,id,&i1,&i2,&i3);
  111.     GetIText(i2,buffer);
  112.     StringToNum(buffer,&n);
  113.     return n;
  114. }
  115.  
  116. /********************************************************************/
  117. /*                                                                    */
  118. /*    Dialog execution                                                */
  119. /*                                                                    */
  120. /********************************************************************/
  121.  
  122. /*    Preferences
  123.  *
  124.  *        This sets the various preferences
  125.  */
  126.  
  127. void Preferences(void)
  128. {
  129.     DialogPtr dlog;
  130.     short i;
  131.     short j;
  132.     short x,y;
  133.     
  134.     dlog = GetMyDialog(129);
  135.     
  136.     SetButton(dlog,3,(SizeFlag == 0) ? 1 : 0);
  137.     SetButton(dlog,4,(SizeFlag == 1) ? 1 : 0);
  138.     SetButton(dlog,5,(SizeFlag == 2) ? 1 : 0);
  139.     SetButton(dlog,6,(BombFlag == 0) ? 1 : 0);
  140.     SetButton(dlog,7,(BombFlag == 1) ? 1 : 0);
  141.     SetButton(dlog,16,(Cruse) ? 1 : 0);
  142.     
  143.     SetValue(dlog,10,SizeX);
  144.     SetValue(dlog,11,SizeY);
  145.     SetValue(dlog,12,BRatio);
  146.     
  147.     for (;;) {
  148.         ModalDialog(NULL,&i);
  149.         switch (i) {
  150.             case 1:
  151.                 SizeX = GetValue(dlog,10);
  152.                 SizeY = GetValue(dlog,11);
  153.                 BRatio = GetValue(dlog,12);
  154.                 for (j = 3; j <= 5; j++) if (GetButton(dlog,j)) break;
  155.                 SizeFlag = j - 3;
  156.                 for (j = 6; j <= 7; j++) if (GetButton(dlog,j)) break;
  157.                 BombFlag = j - 6;
  158.                 
  159.                 Cruse = GetButton(dlog,16);
  160.                 
  161.                 if (SizeX < 5) SizeX = 5;
  162.                 if (SizeX > 40) SizeX = 40;
  163.                 if (SizeY < 5) SizeY = 5;
  164.                 if (SizeY > 40) SizeY = 40;
  165.                 if (BRatio < 5) BRatio = 5;
  166.                 if (BRatio > 40) BRatio = 40;
  167.                 
  168.                 NewMines();
  169.             case 2:
  170.                 DisposDialog(dlog);
  171.                 return;
  172.             case 3:
  173.             case 4:
  174.             case 5:
  175.                 for (j = 3; j <= 5; j++) SetButton(dlog,j,(i == j) ? 1 : 0);
  176.                 switch (i) {
  177.                     case 3:        x = 10, y = 10; break;
  178.                     case 4:        x = 15, y = 15; break;
  179.                     case 5:        x = 30, y = 15; break;
  180.                 }
  181.                 SetValue(dlog,10,x);
  182.                 SetValue(dlog,11,y);
  183.                 break;
  184.             case 6:
  185.             case 7:
  186.                 for (j = 6; j <= 7; j++) SetButton(dlog,j,(i == j) ? 1 : 0);
  187.                 switch (i) {
  188.                     case 6:        x = 10; break;
  189.                     case 7:        x = 5; break;
  190.                 }
  191.                 SetValue(dlog,12,x);
  192.                 break;
  193.             case 10:
  194.             case 11:
  195.                 x = GetValue(dlog,10);
  196.                 y = GetValue(dlog,11);
  197.                 i = 0;
  198.                 if ((x == 10) && (y == 10)) i = 3;
  199.                 if ((x == 15) && (y == 15)) i = 4;
  200.                 if ((x == 30) && (y == 15)) i = 5;
  201.                 for (j = 3; j <= 5; j++) SetButton(dlog,j,(i == j) ? 1 : 0);
  202.                 break;
  203.             case 12:
  204.                 x = GetValue(dlog,12);
  205.                 i = 0;
  206.                 if (x == 10) i = 6;
  207.                 if (x == 5)  i = 7;
  208.                 for (j = 6; j <= 7; j++) SetButton(dlog,j,(i == j) ? 1 : 0);
  209.                 break;
  210.             case 16:
  211.                 SetButton(dlog,16,1 - GetButton(dlog,16));
  212.                 break;
  213.         }
  214.     }
  215. }
  216.  
  217. /*    GetUserName
  218.  *
  219.  *        Who is the user?
  220.  */
  221.  
  222. void GetUserName(char *c)
  223. {
  224.     DialogPtr dlog;
  225.     short i;
  226.     
  227.     dlog = GetMyDialog(132);
  228.     for (;;) {
  229.         ModalDialog(NULL,&i);
  230.         if (i == 1) break;
  231.     }
  232.     GetName(dlog,3,c);
  233.     DisposDialog(dlog);
  234. }
  235.  
  236. /********************************************************************/
  237. /*                                                                    */
  238. /*    Preferences Files                                                */
  239. /*                                                                    */
  240. /********************************************************************/
  241.  
  242. static unsigned char myName[] = "\pMines Preferences";
  243. static unsigned char myFolder[] = "\pPreferences";
  244.  
  245. /*    FindPrefFolder
  246.  *
  247.  *        This determines the DirID of the preferences folder.  If the
  248.  *    preferences folder doesn't exist and can't be created, then
  249.  *  return DirID of the system folder.
  250.  */
  251.  
  252. long FindPrefFolder(void)
  253. {
  254.     SysEnvRec world;
  255.     CInfoPBRec p;
  256.     HParamBlockRec h;
  257.     long d;
  258.     
  259.     /*
  260.      *    Get location and DirID of the system folder
  261.      */
  262.     
  263.     SysEnvirons(1,&world);                                /* Get environs record */
  264.     
  265.     h.wdParam.ioCompletion = NULL;
  266.     h.wdParam.ioVRefNum = world.sysVRefNum;                /* System ref ID */
  267.     h.wdParam.ioWDIndex = 0;
  268.     h.wdParam.ioWDProcID = 'wMNd';
  269.     h.wdParam.ioWDVRefNum = world.sysVRefNum;
  270.     h.wdParam.ioNamePtr = NULL;
  271.     if (PBGetWDInfo((WDPBPtr)&h,0)) return 0;            /* Get system DirID */
  272.     d = h.wdParam.ioWDDirID;                            /* Store it away */
  273.  
  274.     /*
  275.      *    Figure out if the preferences directory exists.  If it doesn't, then
  276.      *    try to create it
  277.      */
  278.     
  279.     p.dirInfo.ioCompletion = NULL;
  280.     p.dirInfo.ioNamePtr = (unsigned char *)myFolder;
  281.     p.dirInfo.ioVRefNum = world.sysVRefNum;                /* System Folder */
  282.     p.dirInfo.ioFDirIndex = 0;                            /* Look up by work dir */
  283.     p.dirInfo.ioDrDirID = 0;                            /* Ignored? */
  284.     if (PBGetCatInfo(&p,0)) {                            /* Get dir ID if there */
  285.         /*
  286.          *    the Preferences folder doesn't exist.  Create it
  287.          */
  288.  
  289.         h.fileParam.ioCompletion = NULL;
  290.         h.fileParam.ioNamePtr = (unsigned char *)myFolder;
  291.         h.fileParam.ioVRefNum = world.sysVRefNum;
  292.         h.fileParam.ioDirID = d;                        /* System folder */
  293.         if (PBDirCreate(&h,0)) return d;                /* Return system folder ID */
  294.         
  295.         return h.fileParam.ioDirID;                        /* Return *my* Dir ID */
  296.     } else return p.dirInfo.ioDrDirID;                    /* PBCat worked... */
  297. }
  298.  
  299. /*    LoadPreferences
  300.  *
  301.  *        This loads the preferences file
  302.  */
  303.  
  304. void LoadPreferences(void)
  305. {
  306.     HParamBlockRec h;
  307.     PrefFiles pf;
  308.     long l;
  309.     SysEnvRec world;
  310.     short x;
  311.  
  312.     SysEnvirons(1,&world);                                /* Get environs record */
  313.     if (world.machineType < 0) {
  314.         /*
  315.          *    HFS Doesn't exist.  Don't use a preferences file.
  316.          */
  317.         
  318.         SizeX = 10;
  319.         SizeY = 10;
  320.         SizeFlag = 0;
  321.         BRatio = 5;
  322.         BombFlag = 1;
  323.         XLoc = 2;
  324.         YLoc = 38;
  325.         
  326.         Cruse = 0;
  327.         
  328.         for (x = 0; x < NUMSCORE; x++) {
  329.             Scores[x].name[0] = '\0';
  330.             Scores[x].time = 1000;
  331.         }
  332.         return;
  333.     }
  334.     
  335.     h.ioParam.ioCompletion = NULL;
  336.     h.ioParam.ioNamePtr = (unsigned char *)myName;
  337.     h.ioParam.ioVRefNum = world.sysVRefNum;
  338.     h.ioParam.ioPermssn = fsRdPerm;                        /* Read only */
  339.     h.ioParam.ioMisc = NULL;
  340.     h.fileParam.ioDirID = FindPrefFolder();                /* Preferences folder... */
  341.     if (PBHOpen(&h,0)) {
  342.         SizeX = 10;
  343.         SizeY = 10;
  344.         SizeFlag = 0;
  345.         BRatio = 5;
  346.         BombFlag = 1;
  347.         
  348.         XLoc = 2;
  349.         YLoc = 38;
  350.         
  351.         Cruse = 0;
  352.         
  353.         for (x = 0; x < NUMSCORE; x++) {
  354.             Scores[x].name[0] = '\0';
  355.             Scores[x].time = 1000;
  356.         }
  357.         return;                                            /* File didn't exist */
  358.     }
  359.  
  360.     l = sizeof(pf);
  361.     FSRead(h.ioParam.ioRefNum,&l,(Ptr)&pf);
  362.     FSClose(h.ioParam.ioRefNum);                        /* Read and close file */
  363.     
  364.     SizeX = pf.SizeX;
  365.     SizeY = pf.SizeY;
  366.     SizeFlag = pf.SizeFlag;
  367.     BRatio = pf.BRatio;
  368.     BombFlag = pf.BombFlag;
  369.     
  370.     XLoc = pf.XWindLoc;
  371.     YLoc = pf.YWindLoc;
  372.     Cruse = pf.Cruse;
  373.     for (x = 0; x < NUMSCORE; x++) Scores[x] = pf.Scores[x];
  374. }
  375.  
  376. /*    SavePreferences
  377.  *
  378.  *        Save preferences file
  379.  */
  380.  
  381. void SavePreferences(void)
  382. {
  383.     HParamBlockRec h;
  384.     PrefFiles pf;
  385.     long l;
  386.     short err;
  387.     SysEnvRec world;
  388.     short x;
  389.     
  390.     SysEnvirons(1,&world);                                /* Get environs record */
  391.     if (world.machineType < 0) return;                    /* No HFS; don't save prefs */
  392.     
  393.     pf.SizeX = SizeX;
  394.     pf.SizeY = SizeY;
  395.     pf.SizeFlag = SizeFlag;
  396.     pf.BRatio = BRatio;
  397.     pf.BombFlag = BombFlag;
  398.     pf.XWindLoc = MineWindow->portRect.left - MineWindow->portBits.bounds.left;
  399.     pf.YWindLoc = MineWindow->portRect.top - MineWindow->portBits.bounds.top;
  400.     pf.Cruse = Cruse;
  401.     for (x = 0; x < NUMSCORE; x++) pf.Scores[x] = Scores[x];
  402.  
  403.     h.ioParam.ioCompletion = NULL;
  404.     h.ioParam.ioNamePtr = (unsigned char *)myName;
  405.     h.ioParam.ioVRefNum = world.sysVRefNum;
  406.     h.ioParam.ioPermssn = fsWrPerm;                        /* Read only */
  407.     h.ioParam.ioMisc = NULL;
  408.     h.fileParam.ioDirID = FindPrefFolder();                /* Preferences folder... */
  409.     err = PBHOpen(&h,0);
  410.     if (err) {
  411.         err = PBHCreate(&h,0);
  412.         if (err) return;
  413.         if (PBHOpen(&h,0)) return;                        /* Try to open it... */
  414.     }
  415.     
  416.     SetEOF(h.ioParam.ioRefNum,0L);
  417.     l = sizeof(pf);
  418.     FSWrite(h.ioParam.ioRefNum,&l,(Ptr)&pf);
  419.     FSClose(h.ioParam.ioRefNum);
  420.     
  421.     l = h.fileParam.ioDirID;                            /* Store away DirID... */    
  422.     PBHGetFInfo(&h,0);                                    /* Get finder info */
  423.     h.fileParam.ioFlFndrInfo.fdType = 'Pref';            /* Preferences file */
  424.     h.fileParam.ioFlFndrInfo.fdCreator = 'wMNd';        /* Mines package */
  425.     h.fileParam.ioDirID = l;                            /* Put back DirID */
  426.     PBHSetFInfo(&h,0);
  427. }
  428.